

/*------------------------------------------------------------------------------
	1. Housekeeping
------------------------------------------------------------------------------*/

	*Run housekeeping code
		qui do ".../replication_package/housekeeping.do"

	*Write log
		log using "$log_loc/claim_rate_by_state.log", text replace

/*------------------------------------------------------------------------------
	2. Clean Main Datasets
------------------------------------------------------------------------------*/
		
		*Import Data
		local year = 2018
		use "$data/childrens`year'.dta", clear

			/*Restrict Sample*/
				*Keep 1095 filings
					rename state_insurance state
					fmerge m:1 state using "$data/statelist.dta"
						assert _merge != 2 //Confirm that all 51 states are in dataset
						drop if _merge != 3

				*Keep if age_num is within the range
					keep if age_num >= 0 & age_num <= 16

				*Keep if kid died after end of year or is currently alive
					keep if `year'*10000+1231<death_date|death_date==0

				*Keep if months covered is possible
					keep if num_cov_ins > 0 & num_cov_ins <= 12

			/*Generate variables for figures*/	
				*Generate outcome variable on claiming
					gen onreturn = file_inc != .

				*Generate a count variable
					gen ct = 1

				*Collapse to the by-age_num level		
					collapse (sum) ct onreturn, by(state)

				*Adjust for claiming limits
					replace onreturn = onreturn * 1.0017029

				*Generate outcome (i.e., percent of children claimed)
					gen perc_return = onreturn/ct

				*Adjust outcome to be out of 100
					replace perc_return = perc_return*100


/*------------------------------------------------------------------------------
	3. Create Figure 2
------------------------------------------------------------------------------*/
			/*Generate Figures */
				*Create Map with results
					maptile perc_return, geography(state) nq(10) spopt(legstyle(2)) twopt(legend(size(small))) legdecimals(1)

				*Export Graph
					graph export "${output}/`year' state child claim.png", replace as(png)


				*Display smallest and largest % Change for states
					sort perc_return
						local state_low_pct = state[1]
						local state_high_pct = state[_N]
					di "The state with the lowest percent claim is `state_low_pct'. The state with the highest percent claim is `state_high_pct'."
